/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ventanas;
import controlador.CtrlVista;
import excepciones.SalaException;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import negocio.Sala;

/**
 *
 * @author Diego
 */
public class FrmSala extends javax.swing.JDialog {

    /**
     * Creates new form FrmSala
     */
    
    private CtrlVista vista;// = new CtrlVista();
    private Sala unaSala;
    
    DefaultListModel modelSalas = new DefaultListModel();
    
    
    
    public FrmSala(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try{
            this.vista = new CtrlVista();
        }
        catch(Exception e){            
            JOptionPane.showMessageDialog(this, "Error de Inicializacion");
        }
        this.setTitle("Salas del Complejo " + CtrlVista.getUnComplejo().getNombre());
        this.inicializarComponentes();
    }
    
    
    
    public void inicializarComponentes(){
        this.txtNombre.setText("");
        this.txtNombre.setEnabled(false);        
        this.txtCapacidad.setText("");
        this.txtCapacidad.setEnabled(false);        
        this.btnBorrar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnGuardar.setEnabled(false);
        this.btnNuevo.setEnabled(true);
        this.RdbActiva.setEnabled(false);
        this.rdbDeshabilitada.setEnabled(false);
        this.RdbActiva.setSelected(false);
        this.rdbDeshabilitada.setSelected(false);
        this.lstSalas.setModel(modelSalas);
        this.cargarSalas();
    }    
    
    public void cargarSalas(){
        this.modelSalas.clear();
        this.lstSalas.setModel(modelSalas);
        if(this.vista.obtenerSalas().size() > 0){
            Iterator it = this.vista.obtenerSalas().iterator();
            while(it.hasNext()){
                unaSala = (Sala) it.next();
                this.modelSalas.addElement(unaSala);
            }
            unaSala = null;
        }else{
            this.modelSalas.addElement("No Se encontraron Salas");
        }
        
    }
    public void seleccionarSala(){
        this.unaSala = (Sala) this.modelSalas.getElementAt(this.lstSalas.getSelectedIndex());
        this.txtCapacidad.setText(this.unaSala.getCapacidad().toString());
        this.txtNombre.setText(this.unaSala.getNombre());
        if(this.unaSala.isEstado()){
            this.RdbActiva.setSelected(true);
            this.rdbDeshabilitada.setSelected(false);
        }else{
            this.RdbActiva.setSelected(false);
            this.rdbDeshabilitada.setSelected(true);
        }
        this.btnNuevo.setEnabled(false);
        this.btnBorrar.setEnabled(true);
        this.btnGuardar.setEnabled(true);
        this.btnEditar.setEnabled(true);
    }
    
    public void nuevo(){
        this.txtNombre.setEnabled(true);
        this.txtNombre.setText("");
        this.txtCapacidad.setEnabled(true);
        this.txtCapacidad.setText("");        
        this.btnBorrar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnGuardar.setEnabled(true);
        this.btnNuevo.setEnabled(false);
        this.rdbDeshabilitada.setEnabled(true);
        this.rdbDeshabilitada.setSelected(true);
        this.RdbActiva.setEnabled(true);
        this.RdbActiva.setEnabled(false);
    }
    
    public void editar(){
     this.txtCapacidad.setEnabled(true);
     this.txtNombre.setEnabled(true);
     this.RdbActiva.setEnabled(true);
     this.rdbDeshabilitada.setEnabled(true);
    }
    
    public void borrar(){
       if(this.unaSala != null){
        int respuesta = JOptionPane.showConfirmDialog(null, "¿Esta accion deshablitara la sala?", "Confirmación", JOptionPane.YES_NO_OPTION);
        switch(respuesta) {
            case JOptionPane.YES_OPTION:
                //--- Operaciones en caso afirmativo
                this.unaSala.setEstado(false);
                this.rdbDeshabilitada.setSelected(true);
                this.RdbActiva.setSelected(false);
                this.guardar();
                break;
        }     
       } 
    }
    
    public void guardar(){
        
        String nombre = this.txtNombre.getText().trim();        
        if (!nombre.isEmpty()){
            try {
                boolean estado = true;
                if(this.RdbActiva.isSelected()){
                    estado = true;
                }else{
                    estado = false;
                }
                Integer capacidad = Integer.valueOf(this.txtCapacidad.getText());
                if (this.unaSala == null){                    
                    this.vista.agregarSala(this.txtNombre.getText(),capacidad,estado);
                }else{                    
                    this.vista.guardarSala(this.txtNombre.getText(),capacidad,estado);
                }
            JOptionPane.showMessageDialog(this, "Sala cargada correctamente");
            
            }
            catch(SalaException e){
                 JOptionPane.showMessageDialog(this, e.getMessage());
            }
            catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
            finally{
                this.inicializarComponentes() ;
            }
        }else{
            JOptionPane.showMessageDialog(this, "Ingrese un nombre de Sala");
        }
        
    }
    public void cerrar(){
        int respuesta = JOptionPane.showConfirmDialog(null, "¿Está seguro que desea salir?", "Confirmación", JOptionPane.YES_NO_OPTION);
        switch(respuesta) {
            case JOptionPane.YES_OPTION:
                //--- Operaciones en caso afirmativo
                this.dispose();
                break;
        }  
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        RdbActiva = new javax.swing.JRadioButton();
        rdbDeshabilitada = new javax.swing.JRadioButton();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtCapacidad = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSalas = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Salas");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Nombre");

        RdbActiva.setText("Activa");
        RdbActiva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RdbActivaMouseClicked(evt);
            }
        });

        rdbDeshabilitada.setText("Deshabilitada");
        rdbDeshabilitada.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdbDeshabilitadaMouseClicked(evt);
            }
        });

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnBorrar.setText("Borrar");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Capacidad");

        txtCapacidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCapacidadActionPerformed(evt);
            }
        });

        lstSalas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSalas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSalasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstSalas);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(RdbActiva)
                                        .addGap(11, 11, 11)
                                        .addComponent(rdbDeshabilitada))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnNuevo)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnEditar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnBorrar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnGuardar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3))
                                        .addGap(13, 13, 13)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(RdbActiva)
                            .addComponent(rdbDeshabilitada))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnNuevo)
                            .addComponent(btnEditar)
                            .addComponent(btnBorrar)
                            .addComponent(btnGuardar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        this.nuevo();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        // TODO add your handling code here:
        this.editar();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        // TODO add your handling code here:
        this.borrar();
    }//GEN-LAST:event_btnBorrarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        this.guardar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtCapacidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCapacidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCapacidadActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.cerrar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void lstSalasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSalasMouseClicked
        // TODO add your handling code here:
        this.seleccionarSala();
    }//GEN-LAST:event_lstSalasMouseClicked

    private void RdbActivaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RdbActivaMouseClicked
        // TODO add your handling code here:
        this.rdbDeshabilitada.setSelected(false);
    }//GEN-LAST:event_RdbActivaMouseClicked

    private void rdbDeshabilitadaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdbDeshabilitadaMouseClicked
        // TODO add your handling code here:
        this.RdbActiva.setSelected(false);
    }//GEN-LAST:event_rdbDeshabilitadaMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main2(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmSala.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmSala.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmSala.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmSala.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmSala dialog = new FrmSala(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton RdbActiva;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstSalas;
    private javax.swing.JRadioButton rdbDeshabilitada;
    private javax.swing.JTextField txtCapacidad;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
